package cn.dkdoo.blackhorse_transactional.other;

import cn.dkdoo.utils.utils.FileUtils;

/**
 * @author nicky
 * @date 2021/5/14 下午6:43
 */
public class InitFileUtils {
//    @Test
    public void testInit() {
        String fileName = "/home/nicky/work_space/tjs-workspace/blackhorse_transactional/src/main/java/cn/dkdoo/utils/templateDefault/redis/RollbackListOperations.java";
//        String fileName = "/home/nicky/work_space/tjs-workspace/blackhorse_transactional/src/main/java/cn/dkdoo/utils/templateDefault/redis/RollbackRedisTemplate.java";
        String[] fileStrs = FileUtils.toArrayByFileReader(fileName);
        for (int i = 0; i < fileStrs.length; i++) {
            String fileStr = fileStrs[i];
            //System.out.println(fileStr);
            if (fileStr.indexOf("Override") != -1) {
                String newFileStr = "";
                String[] nextLines = fileStrs[i + 1].split(" ");
                String methodName = "";
                String paramName = "";
                String returnType = null;
                for (int i1 = 0; i1 < nextLines.length; i1++) {
                    String nextLine = nextLines[i1];
                    if (nextLine.indexOf("public") != -1 && fileStrs[i + 1].indexOf("void") == -1) {
                        returnType = nextLines[i1 + 1];
                    }
                    if (nextLine.indexOf(",") != -1) {
                        paramName += (nextLine.replaceAll(",", "") + ", ");
                    }
                    if (nextLine.indexOf("(") != -1) {
                        String[] items = nextLine.split("\\(");
                        methodName = items[0];
                    } else if (nextLine.indexOf(")") != -1) {
                        String[] items = nextLine.split("\\)");
                        paramName += (items[0] + ", ");
                    }
                }
                paramName = paramName.length() != 0 ? paramName.substring(0, paramName.length() - 2) : paramName;
                //System.out.println("methodName: " + methodName + " paramName: " + paramName);
                String resturnStr = fileStrs[i + 2];

                newFileStr += returnType != null ? ("        " + returnType + " res = ") : "        ";
                newFileStr += "this.template.opsForList()." + methodName + "(" + paramName + ");\n";
                newFileStr += "        this.templateRollback." + methodName + "(" + paramName + ");\n";
                newFileStr += returnType != null ? ("return res;") : "";
                //RollbackRedisTemplate
//                newFileStr = "        String methodName = \"" + methodName + "\";\n Collection<Object> args = new ArrayList<>();\n";
//                String[] paramNames = paramName.split(",");
//                for (String name : paramNames) {
//                    newFileStr += "args.add(" + name + ");\n";
//                }
//                newFileStr+=" super.addRollbackData(methodName, args)\n";

                fileStrs[i + 2] = "        " + newFileStr;

            }
            //System.out.println(fileStrs[i]);
        }
        FileUtils.coverOldFile(fileName, fileStrs);
    }
}
